#include<AT89X52.h>
#define uint unsigned int
#define BEEF P3_6
 uint table[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
 uint Buf[6]={0x40,0x40,0x40,0x40,0x40,0x40};
 uint PassWord[6]={0x3f,0x3f,0x3f,0x3f,0x3f,0x3f};
 uint Tcount,Key,i,k,Def,m,Del=0,j,a=9,b=9,Dead=1,Sum=0;
 void Delay(uint n)
    {
       uint p,q;
       for(p=0;p<n;p++)
       for(q=0;q<n;q++)
         {}
    }
 void KeyFunction(void)
 {
    if(Key!=-1 && Def==0 && Key<10)
	  {  
		if(i<6)
		 {
           Buf[i]=table[Key];
		   if(PassWord[i]==Buf[i] && Del==0)
		      {
		         k++;
		      } 
		    	i++;
	      }
	     Key=-1;
	  }
    if(Key==14 && i>0 && Def==0&& m==0)
       {
          i--;
          if(Del==0)
             {
                if(PassWord[i]==Buf[i])
                  {
                      k--;
                  }
                 Buf[i]=0x40;
             }
          else if(Del==1)
            {
               Buf[i]=0x08;
            }
           Key=-1;
      }
    if(Key==13 && i==6 && Del==1)
	  {
	    for(j=0;j<6;j++)
	       {
	          PassWord[j]=Buf[j];
	       }
	       i=0;
	       Del=0;
	       Key=-1;
    	   k=0;
           Dead=1;
           a=9;
           b=9;
	    Buf[0]=Buf[1]=Buf[2]=Buf[3]=Buf[4]=Buf[5]=0x40;
   	  }
if(i>5)
	   {
	      if(k==6 && Key==13 && Del==0)
	         {
                  Buf[0]=Buf[1]=Buf[2]=Buf[3]=Buf[4]=0x06;
		    	  Buf[5]=0x86;
			      m=1;
		    	  Dead=2;
             }
	  	  else if(k!=6 && Key==13 && Del==0)
		      {
                  Buf[0]=Buf[1]=Buf[2]=Buf[3]=Buf[4]=0x3f;
		    	  Buf[5]=0xbf;
			      m=2;
			  }
	     }
	if(Key==15)
	  {
	      Key=-1;
	      i=0;
          Buf[0]=Buf[1]=Buf[2]=Buf[3]=Buf[4]=Buf[5]=0x40;
	      k=0;
		  m=0;
		  Del=0;
		  Dead=1;
	  }
	if(Key==12 && m==1)
	  {
	      Del=1;
	      i=0;
    	  m=0;
	      Buf[0]=Buf[1]=Buf[2]=Buf[3]=Buf[4]=0x08;
	      Buf[5]=0x88;
          BEEF=0;
		  Delay(150);
    	  BEEF=1;
	  }
 }
 void Timer0Init(void)
   {
       TH0=0x0f0;  
       TL0=0x05f;
       TMOD=0x01;   
       TCON=0x10;   
       IE=0x82;   
   }
void KeyScan()
 {
	uint i,j;
    P2=0xf0;
    if(P2==0xf0)
        {
		    Def=0;
		}
    else
    {
      switch(P2)
        {
          case 0x70:i=0;break;
          case 0xb0:i=1;break;
          case 0xd0:i=2;break;
          case 0xe0:i=3;break;
        }
      P2=0x0f;
      switch(P2)
        {
            case 0x07:j=3;break;
            case 0x0b:j=2;break;
            case 0x0d:j=1;break;
            case 0x0e:j=0;break;
        }
		Def=1;
        Key=4*i+j;
     Delay(100);
    }
 }
void time0_over() interrupt 1
 {
    Tcount++;
	 if(Tcount==2)
    	 {
	         P1=0x7f;
	         P0=Buf[5];
    	 }
	 if(Tcount==3)
    	 {
	         P1=0xbf;
	         P0=Buf[4];	 
	     }
     if(Tcount==4)
	     {
	         P1=0xdf;
	         P0=Buf[3];	 
         }
	 if(Tcount==5)
	     {
	         P1=0xef;
	         P0=Buf[2];	 
	     }
	 if(Tcount==6)
  	     {
 	        P1=0xf7;
	        P0=Buf[1];	 
	     }
	 if(Tcount==7)
	     {
        	P1=0xfb;
	        P0=Buf[0];
	     }
	 if(Tcount==8)
	     {
	        if(Dead==1)
	            {
			      Sum++;
			    }
	        if(Sum==130)
	            {
              	  a--;
	              Sum=0;
	            }
	         P1=0xfd;
        	 P0=table[a];
	     }
	 if(a==-1)
	     {
	        b--;
	        a=9;
	     }
	 if(Tcount==9)
	     {
	        P1=0xfe;
	        P0=table[b];
	        Tcount=0;
	     }
	 if(a==0 && b==0)
	     {
         	 Dead=0;
        	 Key=-1;
        	 BEEF=0;
	     }
      TH0=0xfa;
      TL0=0xff;
 }

void main()
   {
      Tcount=0;
      Key=-1;
      i=0;
      k=0;
      Def=0;
      m=0;
      Timer0Init();
	  while(1)
	    {
	       if(Dead!=0)
	         {
	             KeyScan();
	         }
	       KeyFunction();
	   }
   }
